package com.kaifamiao.www.Thread.do0927;

public class Test {
    public static void main(String[] args) {
        Object lock = new Object();
        Plus plus = new Plus("plus一",lock);
        Plus plus1 = new Plus("plus二",lock);
        Minus minus = new Minus("minus一",lock);
        Minus minus1 = new Minus("minus二",lock);

        plus.start();
        plus1.start();
        minus.start();
        minus1.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("=======");
        System.out.println(Plus.j);
    }
}
class Plus extends Thread{
    public static int j =0;
    private Object lock;
    public Plus(String name){
            super(name);
    }
    public Plus(String name,Object lock){
        super(name);
        this.lock =lock;
    }
    @Override
    public void run(){
       for (int i = 0;i<100;i++){
           try {
               Thread.sleep(10);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           synchronized (lock){
               System.out.println(Thread.currentThread().getName()+"对j加一后是："+ ++j);
           }
       }
    }
}
class Minus extends Thread{
    private Object lock;
    public Minus(String name){
        super(name);
    }
    public Minus(String name,Object lock){
        super(name);
        this.lock = lock;
    }
    @Override
    public void run(){
        for (int i = 0;i<100;i++){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lock){
                System.out.println(Thread.currentThread().getName()+"对j减一后是："+ -- Plus.j);
            }
        }
    }
}